﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace Mimoza.DateTimePlugin
{
    public class ExecutePlugin : Mimoza.PluginLayer.IExecutePlugin
    {
        #region IExecutePlugin Members

        public string PluginId
        {
            get { return this.ToString(); }
        }

        public void Add(Mimoza.ObjectLayer.Policy policy)
        {
            Add(policy, GetData(policy.Data));
        }

        public void Remove(Mimoza.ObjectLayer.Policy policy)
        {
            Remove(policy, GetData(policy.Data));
        }

        public void Update(Mimoza.ObjectLayer.Policy policy)
        {
            Update(policy, GetData(policy.Data));
        }

        #endregion

        void Add(Mimoza.ObjectLayer.Policy policy, DateTimePluginData data)
        {
            Trace("Add", policy, data);

            SetTimeZone(data);

            SaveSynchronizeFlag(data.IsTimeSynchronize);
        }

        void Update(Mimoza.ObjectLayer.Policy policy, DateTimePluginData data)
        {
            Trace("Update", policy, data);

            SetTimeZone(data);

            SaveSynchronizeFlag(data.IsTimeSynchronize);
        }

        void Remove(Mimoza.ObjectLayer.Policy policy, DateTimePluginData data)
        {
            Trace("Remove", policy, data);

            SaveSynchronizeFlag(false);
        }

        private static void SetTimeZone(DateTimePluginData data)
        {
            TimeInfo.TimeInfos.TimeZoneInformation tzi = TimeInfo.TimeInfos.GetTimeZone();

            Common.Logger.Log.Info("Mimoza.DateTimePlugin.ExecutePlugin.SetTimeZone currTimeZone - '" + tzi.standardName + 
                "' New time zone - '" + data.TimeZoneStandartName + "'.");

            if (tzi.standardName != data.TimeZoneStandartName && !string.IsNullOrEmpty(data.TimeZoneStandartName))
            {
                TimeZoneData.Set(data.TimeZoneId);
            }
        }


        private static void SaveSynchronizeFlag(bool bSyncFlag)
        {
            Mimoza.AgentLayer.Settings settings = Mimoza.AgentLayer.Settings.GlobalSettings;
            if (settings != null && settings.IsTimeSynchronizeFromServer != bSyncFlag)
            {
                settings.IsTimeSynchronizeFromServer = bSyncFlag;
                Mimoza.AgentLayer.Settings.Save(settings);
            }
        }

        void Trace(string operationName, Mimoza.ObjectLayer.Policy policy, DateTimePluginData data)
        {
            string registryDataStr = (data.RegistryData == null) ? "null" : data.RegistryData.Length.ToString(); 
            Common.Logger.Log.Info("Mimoza.DateTimePlugin.ExecutePlugin." + operationName + 
                " Name - '" + policy.Name +
                "' IsTimeSynchronize - '" + data.IsTimeSynchronize +
                "' TimeZoneId  - '" + data.TimeZoneStandartName +
                "' TimeZoneTimeSpan  - '" + data.BaseUtcOffsetSec.ToString() +
                " RegistryData - " + registryDataStr + " .");
        }

        DateTimePluginData GetData(string data)
        {
            return DateTimePluginData.Deserialize(data);
        }
    }
}
